package test22;

import javax.sound.midi.SysexMessage;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-12-14
 * Time: 17:59
 */
public class Main {
    static Map<String, Goods> goodsMap = new HashMap<>();
    static Iterator<Goods> it = goodsMap.values().iterator();
    //添加商品
    public static boolean addGoods() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品信息");
        System.out.println("请输入商品编号: ");
        String goodsId = sc.next();
        System.out.println("请输入商品名称: ");
        String goodsName = sc.next();
        System.out.println("请输入商品价格: ");
        double goodsPrice = sc.nextDouble();
        Goods goods = new Goods(goodsId, goodsName, goodsPrice);
        if (goodsMap.containsValue(goodsName)) {
            System.out.println("输入的商品已存在,请重新输入");
            return false;
        }
        goodsMap.put(goodsId, goods);
        System.out.println("商品信息添加成功,信息如下");
        System.out.println(goods.toString());
        return true;
    }
    
    //删除商品
    public static boolean deleteGoods(String goodsName){
        Iterator<Goods> it = goodsMap.values().iterator();
        while(it.hasNext()){
            Goods goods = it.next();
           if(goods.getName().equals(goodsName)){
               goodsMap.remove(goods);
               return true;
            }
        }
        return false;
    }

    //查找商品
    public static Goods findGoods(String goodsName){
        Iterator<Goods> it = goodsMap.values().iterator();
        Goods goods =null;
        while(it.hasNext()){
             goods = it.next();
            if(goods.getName().equals(goodsName)){
                return goods;
            }
        }

        return goods;
    }
    
    //修改商品
    public static boolean modifyGoods(String goodsName){
        Scanner sc = new Scanner(System.in);
        Iterator<Goods> it = goodsMap.values().iterator();
        Goods goods =null;
        while(it.hasNext()){
            goods = it.next();
            if(goods.getName().equals(goodsName)){

                System.out.println("请输入你要修改的商品名称:");
                String name = sc.next();
                System.out.println("请输入你要修改的商品价格:");
                Double price = sc.nextDouble();
                Goods newgood = new Goods(goods.getId(), name,price);
                goodsMap.replace(goods.getId(), goods,newgood);
                System.out.println("商品信息修改成功,信息如下");
                System.out.println(goods.toString());
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        List<Goods> list = new ArrayList<>();
        Goods good1 = new Goods("123","毛巾",25.5);
        Goods good2 = new Goods("124","水杯",30.5);
        Goods good3 = new Goods("125","充电器",33);
        Goods good4= new Goods("126","衣服",5);
        list.add(good1);
        list.add(good2);
        list.add(good3);
        list.add(good4);
        Collections.sort(list);
        //根据名称排序后
        for (Goods goods:list) {
            System.out.println(goods.toString());
        }
    }

}
